//js判断数据类型
const DataType = {
	isFunction(fn) {
		return this.typeOf(fn) === "function";
	},

	isArray(arr) {
		return this.typeOf(arr) === "array";
	},

	isObject(obj) {
		return this.typeOf(obj) === "object";
	},

	isString(str) {
		return this.typeOf(str) === "string";
	},

	isNumber(num) {
		return this.typeOf(num) === "number";
	},

	isDate(date) {
		return this.typeOf(date) === "date";
	},

	isSymbol(symbol) {
		return this.typeOf(symbol) === "symbol";
	},

	isNotNull(val) {
		return val !== undefined && val !== null;
	},

	isNotNullObject(obj) {
		return this.typeOf(obj) === "object" && Object.keys(obj).length > 0;
	},

	hasNotNullKey(obj, key) {
		return obj.hasOwnProperty(key) && this.isNotNull(obj[key]);
	},

	typeOf(data) {
		const dataType = Object.prototype.toString.call(data).replace(/\[object\s(.+)]/, "$1").toLowerCase();
		return data instanceof Element ? "element" : dataType;
	},

	matchSuffix(fileName) {
		// 后缀获取
		let suffix = "";
		// 获取类型结果
		let result = "";
		try {
			let flieArr = fileName.split(".");
			suffix = flieArr[flieArr.length - 1];
		} catch (err) {
			suffix = "";
		}
		// fileName无后缀返回 false
		if (!suffix) {
			return false;
		}
		const resourceType = {
			"image": ["png", "jpg", "jpeg", "bmp", "gif"],// 图片格式
			"txt": ["txt"],// 匹配txt
			"excel": ["xls", "xlsx"],// 匹配 excel
			"word": ["doc", "docx"],// 匹配 word
			"pdf": ["pdf"],// 匹配 pdf
			"ppt": ["ppt"],// 匹配 ppt
			"video": ["mp4", "m2v", "mkv"],// 匹配 视频
			"audio": ["mp3", "wav", "wmv"]// 匹配 音频
		};
		let keys = Object.keys(resourceType);
		for (let key of keys) {
			let fileTypes = resourceType[key];
			if (this.match(fileTypes, suffix)) {// 进行匹配判断
				result = key;
				break;
			}
		}
		return result ? result : "other";// 其他 文件类型
	},

	match(array, suffix) {
		return array.some(item => item === suffix);
	}
};
export default DataType;
